package com.word.teacher.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.word.teacher.domain.WordGroup;
import com.word.teacher.domain.WordGroupDict;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.word.common.annotation.Log;
import com.word.common.enums.BusinessType;
import com.word.teacher.domain.Word;
import com.word.teacher.service.IWordService;
import com.word.common.core.controller.BaseController;
import com.word.common.core.domain.AjaxResult;
import com.word.common.utils.poi.ExcelUtil;
import com.word.common.core.page.TableDataInfo;
import com.word.teacher.service.IWordGroupDictService;

/**
 * 单词信息管理Controller
 * 
 * @author wsccvb
 * @date 2025-05-20
 */
@Controller
@RequestMapping("/teacher/wordinfo")
public class WordController extends BaseController
{
    private String prefix = "teacher/wordinfo";

    @Autowired
    private IWordService wordService;
    
    // 添加单词分组服务依赖
    @Autowired
    private IWordGroupDictService wordGroupDictService;

    @RequiresPermissions("teacher:wordinfo:view")
    @GetMapping()
    public String wordinfo()
    {
        return prefix + "/wordinfo";
    }

    /**
     * 查询单词信息管理列表
     */
    @RequiresPermissions("teacher:wordinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Word word, @RequestParam(value = "groupIds", required = false) Long[] groupIds)
    {
        if (groupIds != null) {
            word.setWordGroupIdList(Arrays.asList(groupIds));
        }
        startPage();
        List<Word> list = wordService.selectWordList(word);
        return getDataTable(list);
    }

    /**
     * 导出单词信息管理列表
     */
    @RequiresPermissions("teacher:wordinfo:export")
    @Log(title = "单词信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Word word)
    {
        List<Word> list = wordService.selectWordList(word);
        ExcelUtil<Word> util = new ExcelUtil<Word>(Word.class);
        return util.exportExcel(list, "单词信息管理数据");
    }

    /**
     * 新增单词信息管理
     */
    @RequiresPermissions("teacher:wordinfo:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("word", new Word());
        // 添加单词分组列表到模型
        List<WordGroupDict> wordGroupDictList = new ArrayList<>();
        mmap.put("wordGroupList", wordGroupDictList);
        return prefix + "/add";
    }

    /**
     * 新增保存单词信息管理
     */
    @RequiresPermissions("teacher:wordinfo:add")
    @Log(title = "单词信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Word word, @RequestParam("groupId") Long[] groupIds) {
        word.setWordGroupIdList(Arrays.asList(groupIds));
        return toAjax(wordService.insertWord(word));
    }

    /**
     * 修改单词信息管理
     */
    @RequiresPermissions("teacher:wordinfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Word word = wordService.selectWordById(id);
        mmap.put("word", word);
        return prefix + "/edit";
    }

    /**
     * 修改保存单词信息管理
     */
    @RequiresPermissions("teacher:wordinfo:edit")
    @Log(title = "单词信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Word word, @RequestParam("groupId") Long[] groupIds) {
        word.setWordGroupIdList(Arrays.asList(groupIds));
        return toAjax(wordService.updateWord(word));
    }

    /**
     * 删除单词信息管理
     */
    @RequiresPermissions("teacher:wordinfo:remove")
    @Log(title = "单词信息管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wordService.deleteWordByIds(ids));
    }
}
